import os
import qianfan
from settings import (
  QIANFAN_API_KEY,
  QIANFAN_SECRET_KEY
)
from utils import (
  log_message
)

#【推荐】使用安全认证AK/SK鉴权，通过环境变量初始化认证信息
# 替换下列示例中参数，安全认证Access Key替换your_iam_ak，Secret Key替换your_iam_sk
os.environ["QIANFAN_AK"] = QIANFAN_API_KEY
os.environ["QIANFAN_SK"] = QIANFAN_SECRET_KEY


chat_comp = qianfan.ChatCompletion()

def run_qianfan_model(system_prompt, full_prompt, model_name):
  message = [
    {
      "role": "user",
      "content": system_prompt,
    },
    {
      "role": "assistant",
      "content": "ok",  # 添加一个空的助手消息作为占位符
    },
    {
      "role": "user",
      "content": full_prompt,
    },
  ]
  # 多轮对话
  resp = chat_comp.do(
    model=model_name,
    messages=message,
    temperature=0.2, # 温度越低越稳定，温度越高越有自主意识
  )
  if 'error_code' in resp['body']:
    log_message(resp)
  return resp['body']['result']


